﻿using System.Net.Http;
using System.Threading.Tasks;

using ManageSystemClient.Record;

namespace ManageSystemClient.HttpService
{
    public class UserService
    {
        private readonly HttpServiceBase baseService;

        public UserService(HttpServiceBase baseService)
        {
            this.baseService = baseService;
        }

        /// <summary>
        /// 向服务器请求登录。身份验证由<c>HttpServiceBase</c>自动处理。
        /// </summary>
        /// <param name="adminId">用户名</param>
        /// <param name="password">密码</param>
        /// <returns><c>Task</c>对象</returns>
        /// <exception cref="TaskCanceledException">请求超时</exception>
        /// <exception cref="HttpRequestException">返回了错误代码</exception>
        public async Task LoginAsync(uint adminId, string password)
        {
            var token = await baseService.SendAsync<JwtToken>("/user/login", HttpMethod.Get, new Identity(adminId, password));
            baseService.Authorize(token.Bearer);
        }

        /// <summary>
        /// 向服务器请求登出。
        /// </summary>
        /// <returns><c>Task</c>对象</returns>
        /// <exception cref="TaskCanceledException">请求超时</exception>
        /// <exception cref="HttpRequestException">返回了错误代码</exception>
        public async Task LogoutAsync()
        {
            await baseService.SendAsync("/user/logout", HttpMethod.Put);
        }


        /// <summary>
        /// 向服务器请求注册。
        /// </summary>
        /// <param name="adminId">用户名</param>
        /// <param name="password">密码</param>
        /// <returns><c>Task</c>对象</returns>
        /// <exception cref="TaskCanceledException">请求超时</exception>
        /// <exception cref="HttpRequestException">返回了错误代码</exception>
        public async Task RegisterAsync(uint adminId, string password)
        {
            await baseService.SendAsync("/user/register", HttpMethod.Put, new Identity(adminId, password));
        }
    }
}
